<?php

namespace app\base\controller\api;

use app\base\model\MemberBeanRecords;
use app\base\model\MemberConsumptionRecords;
use app\base\model\MemberDevicePackages;
use app\base\service\ChanjingService;
use app\base\service\MemberService;
use think\admin\Controller;
use app\base\model\MemberPlans;
use app\base\controller\api\Auth;
use app\base\model\MemberBeanPackages;



/**
 * 会员用户相关接口
 * Class MemberShip
 * @package app\base\api\controller
 */
class MemberShip extends Auth
{
   // private $user_id=10002;

    //拉取用户信息
    public function getUserInfo()
    {
        $member_info = $this->app->db->name("ai_user")
            ->field("id,usertype,username,nickname,contact_phone,matrix_count,expire_date,member_plans_id,jc_devices,jl_devices,js_voices,jl_voices,js_videos,jl_videos")
            ->where(['id'=>$this->user_id])->find();
        $guarder = MemberPlans::mQuery()->where(['id'=>$member_info['member_plans_id']])->find();
        $member_info['avatar']=$guarder['logo'];
        $member_info['guarder'] = $guarder;
        //计算剩余几天到期
        $expire_date = strtotime($member_info['expire_date']);
        $expire_day = ceil(($expire_date-time())/86400);
        $member_info['expire_day'] = $expire_day;
        $member_info['bean_balance'] = MemberService::instance()->getUserRemainingBeans($this->user_id);
        //获取有音视频模板数
        $member_info['voices'] = $this->app->db->name("ai_voice")->where(['user_id'=>$this->user_id,'type'=>1,'is_deleted'=>0])->count();
        $member_info['videos'] = $this->app->db->name("ai_voice")->where(['user_id'=>$this->user_id,'type'=>2,'is_deleted'=>0])->count();
        //获取已有设备数量
        $member_info['devices'] = $this->app->db->name("ai_device")->where(['user_id'=>$this->user_id])->count();
        $this->success('获取成功', $member_info);
    }





    //会员套餐列表
    public function getPlansList()
    {
        $version_types = MemberPlans::mQuery()->where(['status'=>1,'is_deleted'=>0])->order('sort asc,id asc')->column("version_type");
        //数组去重
        $version_types = array_unique($version_types);
        $data = [];
        foreach ($version_types as $version_type){
            $list = MemberPlans::mQuery()->where(['status'=>1,'is_deleted'=>0,'version_type'=>$version_type])->order('sort asc,id asc')->select()->toArray();
            $additional_info="";
            $title = "";
            foreach ($list as &$v){
                $v['additional_info'] = explode("|", $v['additional_info']);
                $title = $v['title'];
                //精确到小数点后最后一个为0
                $v['price'] = rtrim(rtrim(sprintf('%.2f', $v['price']), '0'), '.');
                $v['hx_price'] = rtrim(rtrim(sprintf('%.2f', $v['hx_price']), '0'), '.');

            }
            $data[] = [
                'title'=>$title,
                'version_type'=>$version_type,
                'list'=>$list,
            ];
        }

        $this->success('获取成功', $data);
    }
    //会员套餐详情
    public function getPlansInfo()
    {
        $id = input('id');
        $data = MemberPlans::mQuery()->where(['id'=>$id,'status'=>1,'is_deleted'=>0])->find();
        $data['additional_info'] = explode("\r\n", $data['additional_info']);
        $this->success('获取成功', $data);
    }

    //加量包套餐
    public function getBeanPackagesList()
    {
        $list = MemberBeanPackages::mQuery()->where(['status'=>1,'is_deleted'=>0])->order('sort asc,id asc')->select()->toArray();
        foreach ($list as &$v){
            $v['price'] = rtrim(rtrim(sprintf('%.2f', $v['price']), '0'), '.');
            $v['hx_price'] = rtrim(rtrim(sprintf('%.2f', $v['hx_price']), '0'), '.');
            $v['base_beans'] = rtrim(rtrim(sprintf('%.2f', $v['base_beans']), '0'), '.');
            $v['bonus_beans'] = rtrim(rtrim(sprintf('%.2f', $v['bonus_beans']), '0'), '.');
        }
        $this->success('获取成功', $list);
    }
    //加量包套餐详情
    public function getBeanPackagesInfo()
    {
        $id = input('id');
        $data = MemberBeanPackages::mQuery()->where(['id'=>$id,'status'=>1,'is_deleted'=>0])->find();
        $this->success('获取成功', $data);
    }

    //设备套餐列表
    public function getDevicePackagesList(){
        $type = input('type',1);
        $list = MemberDevicePackages::mQuery()->where(['status'=>1,'is_deleted'=>0,'type'=>$type])->order('sort asc,id asc')->select()->toArray();
        foreach ($list as &$v){
            $v['price'] = rtrim(rtrim(sprintf('%.2f', $v['price']), '0'), '.');
            $v['hx_price'] = rtrim(rtrim(sprintf('%.2f', $v['hx_price']), '0'), '.');
        }
        $this->success('获取成功', $list);
    }

    //会员消费记录
    public function getConsumptionRecords()
    {
        $page= input('page',1);
        $page_size = input('page_size',20);
        $list = MemberConsumptionRecords::mQuery()->where(['user_id'=>$this->user_id])
            ->order('id desc')->page($page,false,false,$page_size);
        foreach ($list['list'] as &$v){
            $v['content'] = json_decode($v['content'],true);
        }
        $this->success('获取成功', $list);

    }

    //蓝豆记录列表
    public function getBeanRecords(){
        $page= input('page',1);
        $page_size = input('page_size',20);
        $list = MemberBeanRecords::mQuery()->where(['user_id'=>$this->user_id])->order('expire_date desc')->page($page,false,false,$page_size);
        $this->success('获取成功', $list);
    }

    //获取蓝豆余额
    public function getBeanBalance(){
        $bean_balance = MemberBeanRecords::mQuery()->where(['user_id'=>$this->user_id])
            ->whereTime('expire_date','>',date('Y-m-d H:i:s'))->sum('remaining_beans');
        //获取近30天即将到期的蓝豆数
        $bean_count = MemberBeanRecords::mQuery()->where(['user_id'=>$this->user_id])
            ->whereTime('expire_date','<',date('Y-m-d H:i:s',strtotime('+30 day')))
            ->whereTime('expire_date','>',date('Y-m-d H:i:s'))
            ->sum('remaining_beans');
        $this->success('获取成功', ['bean_balance'=>$bean_balance,'bean_count'=>$bean_count]);
    }

    //获取某项计价规则
    public function getPriceRule(){
        $data = input();
        $price_rule = $this->app->db->name("member_pricing_rules")->where($data)->find();
        $this->success('获取成功', $price_rule);
    }

}
